#include <REGX52.H>

sbit sg90 = P1^1;//舵机接到P1.0口
int cot   = 0;
int angle;//角度

void InitTime0()		//0.5毫秒@11.0592MHz
{
	TMOD &= 0xF0;    // 清除TMOD的低4位
    TMOD |= 0x01;    // 设置定时器0为模式1 (16位定时器)
	TL0  = 0x1A;		//设置定时初值
	TH0  = 0xFF;		//设置定时初值	
	TR0  = 1;		//定时器0开始计时
	TF0  = 0;	//清除TF0标志
	//启动中断
	EA  = 1;
	ET0 = 1;
	
}

void sg_middle(){

		angle = 6;
		cot = 0; 

}

void sg_right(){

		angle = 3;
		cot = 0; 

}

void sg_left(){

		angle = 9;
		cot = 0; 

}

void T0Handler() interrupt 1{
	
	cot++;
	TL0 = 0x1A;
	TH0 = 0xFF;	
	

	if(cot < angle){
		sg90 = 1;
	}else{
		sg90 = 0;
	}
	
	if(cot == 80){
	
		cot  = 0;
		sg90 = 1;
	}
}